package com.singzi.leetcode.N1800;

import javafx.scene.layout.Priority;

import java.util.PriorityQueue;
import java.util.Queue;

public class T1705 {
    public static void main(String[] args) {
        int[] apples = {1,2,3,5,2};
        int[] days = {3,2,1,4,2};
        Solution1705 solution1705 = new Solution1705();
        System.out.println(solution1705.eatenApples(apples, days));
    }
}
class Solution1705 {

    public int eatenApples(int[] apples, int[] days) {

        int result = 0;
        Queue<int[]> queue = new PriorityQueue<>((a,b)->a[0] - b[0]);
        int index = 0, n = days.length;
        while (!queue.isEmpty() || index < n){
            if(index < n && apples[index] >0){
                queue.add(new int[]{days[index] + index, apples[index]});
            }
            while (!queue.isEmpty() && (queue.peek()[0] > index || queue.peek()[1] == 0)){
                queue.remove();
            }

            if(!queue.isEmpty() && queue.peek()[0] > index){
                result++;
                queue.peek()[1]--;
            }
            index++;
            Integer.bitCount(n);

        }
        return result;



    }
    public int myEatenApples(int[] apples, int[] days) {
        int result = 0;
        int remainApples = 0;
        int indexDays = 1;
        int length = days.length;
        int minDays;
        while (remainApples>0||indexDays<=length){
            remainApples = 0;
            int eatenIndex = -1;
            minDays = 99999;
            //计算现在的总数
            for(int i = 0;i <Math.min(length, indexDays);i++){
                if(days[i]>0&&days[i]<minDays&&apples[i]>0){
                    minDays = days[i];
                    eatenIndex = i;
                }
                if(days[i]>0) days[i]--;
                else continue;
                remainApples += apples[i];

            }
            //是否可以吃掉一个苹果
            if(eatenIndex != -1) {
                apples[eatenIndex]--;
                result++;
            }
                indexDays++;
        }

        return result;
    }
}